<?php  
class ControllerModulelavamenu extends Controller 
{
    protected $category_id = 0;
    protected $path = array();
    
    protected function index() 
    {
        $this->language->load('module/lavamenu');
        
        $this->data['heading_title'] = $this->language->get('heading_title');
        
        $this->load->model('catalog/category');
        $this->load->model('tool/seo_url');
        
        if (isset($this->request->get['path'])) 
        {
            $this->path = explode('_', $this->request->get['path']);
            
            $this->category_id = end($this->path);   
            
            $this->thowzif     = $this->path[0];
        }
        
        $this->data['category'] = $this->getCategories(0);
                                                
        $this->id = 'lavamenu';

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/lavamenu.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/module/lavamenu.tpl';
        } else {
            $this->template = 'default/template/module/lavamenu.tpl';
        }
        
        $this->render();
      }
    
    protected function getCategories($parent_id, $current_path = '') 
    {
        $category_id = array_shift($this->path);
        
        $output = '';
        
        $results = $this->model_catalog_category->getCategories($parent_id);
        
        if ($results) { 
            $output .= '<ul class="menu">';
        }
        
        foreach ($results as $result) 
        {    
            if (!$current_path) 
            {
                $new_path = $result['category_id'];
            } 
            else 
            {
                $new_path = $current_path . '_' . $result['category_id'];
            }
            
            $output .= '<li>';
            
            $children = '';

            $children = $this->getChildCategories($result['category_id'], $new_path);

            if ($this->category_id == $result['category_id'] || $this->thowzif == $result['category_id']) 
            {
                $output .= '<li class="current"><a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $new_path)  . '"><span>' . $result['name'] . '</span></a>';
            } 
            else
            {
                $output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $new_path)  . '"><span>' . $result['name'] . '</span></a>';
            }
            
            //$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path)  . '"><span>' . $result['name'] . '</span></a>';
        
            $output .= $children;
        
            $output .= '</li>'; 
        }
 
        if ($results) 
        {
            $output .= '</ul>';
        }
        
        return $output;
    }
    
    protected function getChildCategories($parent_id, $current_path = '') {
        $category_id = array_shift($this->path);
        
        $output = '';
        
        $results = $this->model_catalog_category->getCategories($parent_id);
        
        if ($results) { 
            $output .= '<div><ul>';
        }
        
        foreach ($results as $result) {    
            if (!$current_path) {
                $new_path = $result['category_id'];
            } else {
                $new_path = $current_path . '_' . $result['category_id'];
            }
            
            $output .= '<li>';
            
            $children = '';
            
            if ($category_id == $result['category_id']) {
                $children = $this->getChildCategories($result['category_id'], $new_path);
            }
            
            if ($this->category_id == $result['category_id']) {
                $output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $new_path)  . '"><span><font color="#30acff">' . $result['name'] . '</font><span></a>';
            } else {
                $output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $new_path)  . '"><span>' . $result['name'] . '</span></a>';
            }
            //$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path)  . '"><span>' . $result['name'] . '</span></a>';
                        
            $output .= $children;
        
            $output .= '</li>'; 
        }
 
        if ($results) {
            $output .= '</ul></div>';
        }
        
        return $output;
    }
}
?>
